import axios, { AxiosResponse } from "axios";
import { StoreModel } from "../models/Store";
import { TakeoutModel } from "../models/Takeout";

class TakeoutApis {
    recommendTakeouts(): Promise<Array<TakeoutModel>> {
        const promise: Promise<Array<TakeoutModel>> = new Promise((resolve, reject) => {
            axios.get('/api/takeout/takeout-recommend')
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code === 200) {
                        let store = reslut.data.data;
                        resolve(store);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }
    queryTakeouts(id: string): Promise<Array<TakeoutModel>> {
        const promise: Promise<Array<TakeoutModel>> = new Promise((resolve, reject) => {
            let store: StoreModel = {
                id: id
            };
            axios.get('/api/takeout/takeout-store', { params: store })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code === 200) {
                        let store = reslut.data.data;
                        resolve(store);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    queryTakeout(id: number): Promise<TakeoutModel> {
        const promise: Promise<TakeoutModel> = new Promise((resolve, reject) => {
            let takeout: TakeoutModel = {
                id: id
            };
            axios.get('/api/takeout/takeout', { params: takeout })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code === 200) {
                        let store = reslut.data.data;
                        resolve(store);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    queryAllTakeout(): Promise<Array<TakeoutModel>> {
        const promise: Promise<Array<TakeoutModel>> = new Promise((resolve, reject) => {
            axios.get('/api/takeout/takeout-all')
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code === 200) {
                        let store = reslut.data.data;
                        resolve(store);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    applyTakeout(takeout: TakeoutModel): Promise<TakeoutModel> {
        const promise: Promise<TakeoutModel> = new Promise((resolve, reject) => {
            axios.post('/api/takeout/takeout', takeout)
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code === 200) {
                        let takeout: TakeoutModel = reslut.data.data;
                        resolve(takeout);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    queryLatestTakeout(): Promise<Array<TakeoutModel>> {
        const promise: Promise<Array<TakeoutModel>> = new Promise((resolve, reject) => {
            axios.get('/api/takeout/takeout-new')
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code === 200) {
                        let takeout: Array<TakeoutModel> = reslut.data.data;
                        resolve(takeout);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    queryTakeoutByName(name: string): Promise<Array<TakeoutModel>> {
        const promise: Promise<Array<TakeoutModel>> = new Promise((resolve, reject) => {
            let obj: object = {
                takeout_name: name
            }
            axios.get('/api/takeout/takeout-name', { params: obj })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code === 200) {
                        let takeout: Array<TakeoutModel> = reslut.data.data;
                        resolve(takeout);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    queryTakeoutByType(id: number): Promise<Array<TakeoutModel>> {
        const promise: Promise<Array<TakeoutModel>> = new Promise((resolve, reject) => {
            let obj: object = {
                kind: id
            }
            axios.get('/api/takeout/takeout-kind', { params: obj })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code === 200) {
                        let takeout: Array<TakeoutModel> = reslut.data.data;
                        resolve(takeout);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }
}

export const takeoutApis: TakeoutApis = new TakeoutApis();